package com.yzt.minemodule.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.arouter.RouterMine;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.sysConfig.ChatDiamondBean;
import com.yzt.zhuju.network.entity.sysConfig.ChatSettingBean;
import com.yzt.zhuju.widget.pickview.WhellPickerViewManager;
import com.yzt.minemodule.R;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.plugin_network.response.HttpResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/11/17
 * desc   : 聊天设置
 */
@SuppressLint("SetTextI18n")
@Route(path = RouterMine.ACTIVITY_CHAT_SETTING)
public class ChatSetActivity extends BaseActivity {

    private CheckBox checkBox;

    private List<ChatDiamondBean> stringList;

    private TextView tvDiamond;

    private String diamond;

    private String str;

    /**
     * 聊天价格配置
     */
    private ChatSettingBean chatSettingBean;

    private LinearLayout llPriceContainer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_chat_setting;
    }

    @Override
    public void installViews() {
        mTitleBar.setTitle(getResources().getString(R.string.chat_setting));

        // 资费
        FrameLayout flDiamondContainer = findViewById(R.id.layout_diamond);
        flDiamondContainer.setOnClickListener(this);

        // 美颜
        findViewById(R.id.layout_beauty).setOnClickListener(this);

        // 接听
        checkBox = findViewById(R.id.cb_video);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) ->
                switchVideo(isChecked)
        );

        // 价格
        tvDiamond = findViewById(R.id.tv_diamond);

        ImageView imgUnit = findViewById(R.id.img_unit);

        llPriceContainer = findViewById(R.id.ll_price_container);

        TextView tvUnit = findViewById(R.id.tv_unit);

        // 获取视频资费列表
        if (RXingUtils.getLoginAccount().getUserInfo().getGender() == 2) {
            flDiamondContainer.setVisibility(View.VISIBLE);
            stringList = new ArrayList<>();
            BizLayer.getInstance().getMineModule().getCallExpensesList(RXingUtils.getUserId(), new AresSubscriber<ChatSettingBean>() {
                @Override
                protected void onSuccess(ChatSettingBean data) {
                    chatSettingBean = data;
                    stringList = data.getVideo().getSetGears();
                    checkBox.setChecked(data.isVideoSwitch());
                    if (data.getVideo() == null || "".equals(data.getVideo().getDiamond())) {
                        llPriceContainer.setVisibility(View.GONE);
                    } else {
                        llPriceContainer.setVisibility(View.VISIBLE);
                    }
                    tvDiamond.setText(data.getVideo().getDiamond());
                    ImageLoadUtil.load(ChatSetActivity.this, data.getVideo().getDiamondUnit(), imgUnit);
                    tvUnit.setText("/" + data.getVideo().getUnit());
                    str = data.getVideo().getDiamondUnit();
                    diamond = data.getVideo().getDiamond();
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.layout_diamond) {
            if (chatSettingBean == null) {
                return;
            }
            setVideoTariff();
        } else if (id == R.id.layout_beauty) {
           // RouterChatRoom.FaceBeautyActivity();
        }
    }

    /**
     * 设置视频资费
     */
    private void setVideoTariff() {

        if (stringList == null) {
            return;
        }

        List<String> listDiamond = new ArrayList<>();
        for (ChatDiamondBean bean : chatSettingBean.getVideo().getSetGears()) {
            listDiamond.add(bean.getContent());
        }

        WhellPickerViewManager whellPickerViewManager = WhellPickerViewManager.getInstance(mActivity).builder(listDiamond);
        whellPickerViewManager.setDefault(diamond + str);
        whellPickerViewManager.showPickerView();
        whellPickerViewManager.setOnCallOptionsListener(new WhellPickerViewManager.OnCallOptionsListener() {
            @Override
            public void callBackInfo(String sw1, String sw2) {

                try {

                    for (int i = 0; i < chatSettingBean.getVideo().getSetGears().size(); i++) {
                        ChatDiamondBean bean = chatSettingBean.getVideo().getSetGears().get(i);
                        if (bean.getContent().equals(sw1)) {
                            diamond = bean.getDiamond();
                        }
                    }

                    BizLayer.getInstance().getMineModule().setCallExpenses(diamond, new AresSubscriber<HttpResponse>() {
                        @Override
                        protected void onSuccess(HttpResponse response) {
                            llPriceContainer.setVisibility(View.VISIBLE);
                            tvDiamond.setText(diamond);
                        }
                    });
                } catch (NumberFormatException ignored) {

                }
            }
        });
    }

    /**
     * 视频开关
     */
    private void switchVideo(boolean open) {
        BizLayer.getInstance().getMineModule().setCallSwitch(open ? 1 : 0, new AresSubscriber<Object>() {
            @Override
            protected void onSuccess(Object response) {
                checkBox.setChecked(open);
            }
        });
    }
}

